package com.itheima.controller;

import com.itheima.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    /*
        方式一:使用@Value挨个注入
    */
    @Value("${name}")
    private String name;

    @Value("${person.name}")
    private String name2;

    @Value("${person.age}")
    private int age;

    @Value("${address[0]}")
    private String address1;

    @Value("${address[1]}")
    private String address2;

    @Value("${msg1}")
    private String msg1;

    @Value("${msg2}")
    private String msg2;

    /*
        方式二:使用Environment接收,自动注入,然后通过getProperty()方法获取
    */
    @Autowired
    private Environment environment;

    /*
        方式三
    */
    @Autowired
    private Person person;

    @RequestMapping("/hello")
    public String hello(){

        System.out.println(name);
        System.out.println(name2);
        System.out.println(age);
        System.out.println(address1);
        System.out.println(address2);
        System.out.println(msg1);
        System.out.println(msg2);

        System.out.println("======================");

        System.out.println(environment.getProperty("name"));
        System.out.println(environment.getProperty("address[0]"));
        System.out.println(environment.getProperty("person.age"));

        System.out.println("======================");

        System.out.println(person.getName());
        System.out.println(person.getAge());
        String[] address = person.getAddress();
        for (String s : address) {
            System.out.println(s);
        }

        return "Hello Spring Boot!";
    }

}
